package org.jeecg.modules.sggg.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sggg.entity.SgBacklog;
import org.jeecg.modules.sggg.service.ISgBacklogService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 待办表
 * @Author: jeecg-boot
 * @Date: 2022-10-17
 * @Version: V1.0
 */
@Api(tags = "待办表")
@RestController
@RequestMapping("/sggg/sgBacklog")
@Slf4j
public class SgBacklogController extends JeecgController<SgBacklog, ISgBacklogService> {

    @Autowired
    private ISgBacklogService sgBacklogService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysBaseApiImpl sysBaseApi;


    @ApiOperation(value = "待办表-分页列表查询", notes = "待办表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgBacklog>> queryPageList(SgBacklog sgBacklog,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SgBacklog> page = new Page<>(pageNo, pageSize);

        //当前用户
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //当前用户角色
        List<String> userRole = sysUserService.getRole(loginUser.getUsername());

        IPage<SgBacklog> pageList;

        if (userRole.contains("admin")) {//管理员
            sgBacklog.setSysOrgCode(loginUser.getOrgCode());
        } else {//非管理员
            sgBacklog.setCommander(loginUser.getId());
        }
        pageList = sgBacklogService.queryList(page, sgBacklog.getSysOrgCode(), sgBacklog.getCommander(), sgBacklog.getUserName(), sgBacklog.getIdCard(), sgBacklog.getPhone(), sgBacklog.getBacklogType(), sgBacklog.getDayNum(), sgBacklog.getOverdue());
        return Result.OK(pageList);
    }


    /**
     * 居民今日逾期待办查询
     *
     * @param id id
     * @return 结果
     */
    @ApiOperation(value = "待办表-居民今日逾期待办查询", notes = "待办表-居民今日逾期待办查询")
    @GetMapping("/queryListById")
    public Result<Object> queryListById(String id) {
        JSONObject jsonObject = new JSONObject();
        List<SgBacklog> list = sgBacklogService.list(
                new QueryWrapper<SgBacklog>()
                        .eq("member_id", id)
                        .eq("status", 0)
                        .last("and to_days(backlog_time) = to_days(now())")
        );
        jsonObject.set("todayList", list);
        list = sgBacklogService.list(
                new QueryWrapper<SgBacklog>()
                        .eq("member_id", id)
                        .eq("status", 0)
                        .last("and date(backlog_time) <current_date()")
        );
        jsonObject.set("overdue", list);
        return Result.OK(jsonObject);
    }


    /**
     * 待分配分页列表查询
     *
     * @param sgBacklog 对象
     * @param pageNo    当前页
     * @param pageSize  页大小
     * @return 当前页数据
     */
    @ApiOperation(value = "待办表-待分配分页列表查询", notes = "待办表-待分配分页列表查询")
    @GetMapping(value = "/futureList")
    public Result<IPage<SgBacklog>> queryFuturePageList(SgBacklog sgBacklog,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SgBacklog> page = new Page<>(pageNo, pageSize);

        //当前用户
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        sgBacklog.setSysOrgCode(loginUser.getOrgCode());
        IPage<SgBacklog> pageList = sgBacklogService.queryFutureList(page, sgBacklog.getSysOrgCode(), sgBacklog.getUserName(), sgBacklog.getIdCard(), sgBacklog.getPhone());
        return Result.OK(pageList);
    }


    @AutoLog(value = "待办表-添加")
    @ApiOperation(value = "待办表-添加", notes = "待办表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgBacklog sgBacklog) {
        //当前用户
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //当前用户角色
        List<String> userRole = sysUserService.getRole(loginUser.getUsername());

        if (userRole.contains("admin")) {
            sgBacklogService.save(sgBacklog);
        } else {
            sgBacklog.setCommander(loginUser.getId());
            sgBacklogService.save(sgBacklog);
        }

        return Result.OK("添加成功！");
    }

    @AutoLog(value = "待办表-编辑")
    @ApiOperation(value = "待办表-编辑", notes = "待办表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgBacklog sgBacklog) {

        sgBacklogService.updateById(sgBacklog);

        //发送方信息
        //LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //接收方信息
        //SysUser user = sysUserService.getById(sgBacklog.getCommander());
        //待办信息
        //SgBacklog sb = sgBacklogService.getById(sgBacklog.getId());
        //分配通知消息
        //MessageDTO messageDTO = new MessageDTO(loginUser.getUsername(), user.getUsername(), "待办事项消息提醒", loginUser.getRealname() + "分配任务:" + sb.getName() + "<a href='/sggg/sgBacklogList'>点击查看待办消息详情</a>", "1");
        //sysBaseApi.sendSysAnnouncement(messageDTO);

        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "待办表-通过id删除")
    @ApiOperation(value = "待办表-通过id删除", notes = "待办表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgBacklogService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "待办表-批量删除")
    @ApiOperation(value = "待办表-批量删除", notes = "待办表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgBacklogService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "待办表-通过id查询", notes = "待办表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgBacklog> queryById(@RequestParam(name = "id") String id) {
        SgBacklog sgBacklog = sgBacklogService.getById(id);
        if (sgBacklog == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgBacklog);
    }

}
